package com.cn;

import org.flowable.engine.ProcessEngine;
import org.flowable.engine.RepositoryService;
import org.flowable.engine.RuntimeService;
import org.flowable.engine.TaskService;
import org.flowable.engine.repository.Deployment;
import org.flowable.engine.runtime.Execution;
import org.flowable.engine.runtime.ExecutionQuery;
import org.flowable.task.api.Task;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 2月14号
 * 测试
 */
@SpringBootTest
public class FlowableDemo01 {

    @Autowired
    private RepositoryService repositoryService;

    @Autowired
    private RuntimeService runtimeService;

    @Autowired
    private TaskService taskService;


    /**
     * 创建项目
     */
    @Test
    void RepositoryServiceTest(){
        Deployment deploy = repositoryService.createDeployment()
                .addClasspathResource("process01-基础/leaveApproval.bpmn20.xml")
                .name("请假审批02")
                .deploy();
        System.out.println(deploy.getId());
    }

    /**
     * 启动项目
     */
    @Test
    void RuntimeServiceTest(){
        HashMap<String, Object> map = new HashMap<>();
        map.put("num","3");
        map.put("proposer","李四");
        map.put("dest","请假回家");
        map.put("time",new Date());
        String id = "approval";
        runtimeService.startProcessInstanceByKey(id,map);
    }


    /**
     * 查询代表任务
     */
    @Test
    void selectUserTask(){
        String name = "李四";
        Task task = taskService.createTaskQuery()
                .taskAssignee(name)
                .singleResult();
        System.out.println(task.toString());
    }

    /**
     * 查询代办任务
     */
    @Test
    void selectUserVariables(){
        //查询变量
        List<Execution> list = runtimeService.createExecutionQuery().list();
        for (Execution execution : list) {
            Map<String, Object> map = runtimeService.getVariables(execution.getId());
            System.out.println(map);
        }
    }


    /**
     * 开始审批
     */
    @Test
    void TaskServiceTest(){
        String id = "b453d8ab-ca1d-11ee-9043-30d04231d1ff";
        taskService.complete(id);
    }


    /**
     * 查询代办任务
     */
    @Test
    void userTaskTest(){
        String name =  "王五";
        Task task = taskService.createTaskQuery()
                .taskName(name)
                .singleResult();
        System.out.println(task);
    }




}
